<?php

/**
 * 文章相关接口
 */
class a_article  extends CI_Controller {

    function __construct() {
        //echo $ba;
        parent::__construct();
        $this->url = $this->uri->segments;
    }

    /**
     * 文章列表
     * @param int $configid 网站id
     * @param int $classid 文章分类id
     * @param int $isrecommend 是否推荐 1 推荐 ，2未推荐 0 所有
     * @param int $page 当前页数
     * @param int $limit 显示数量
     * @return array 
     */
    function get_article_list_page($configid = 0, $classid = 0, $isrecommend = 0, $page = 1, $limit = 10) {
        $where = array(
            'article.isclose' => 0
        );
        if ($configid > 0) {
            $where['class.configid'] = $configid;
        }
        if ($classid > 0) {
            $where['article.classid'] = $classid;
        }
        if ($isrecommend == 1) {
            $where['article.isrecommend'] = 1;
        } elseif ($isrecommend == 2) {
            $where['article.isrecommend'] = 0;
        }

        $data = array(
            'from' => 'article',
            'column' => 'article.articleid,article.classid,article.title,article.smallpic,article.description,article.addtime,class.classname,article.clicknum,article.details',
            'join' => array(
                'class' => 'class.classid=article.classid',
            ),
            'where' => $where,
            'order' => 'article.listid desc',
        );
        $result = $this->m_public->get_list_page($data, $page, $limit);
        return $result;
    }

    /**
     * 文章列表
     * @param int $configid 网站id
     * @param int $classid 文章分类id
     * @param int $isrecommend 是否推荐 1 推荐 ，2未推荐 0 所有
     * @param int $limit 显示数量
     * @return array 
     */
    function get_article_list($configid = 0, $classid = 0, $isrecommend = 0, $page , $limit = 10 , $expand=array()) {
        $where = array(
            'article.isclose' => 0
        );
        if ($configid > 0) {
            $where['class.configid'] = $configid;
        }
        if ($classid > 0) {
            $where['article.classid'] = $classid;
        }
        if ($isrecommend == 1) {
            $where['article.isrecommend'] = 1;
        } elseif ($isrecommend == 2) {
            $where['article.isrecommend'] = 0;
        }
        if(!empty($expand)){
            foreach ($expand as $key => $value) {
                $where[$key] = $value;
            }
        }

        $data = array(
            'from' => 'article',
            'column' => 'article.articleid,article.classid,article.title,article.smallpic,article.bigpic,article.description,class.classname',
            'join' => array(
                'class' => 'class.classid=article.classid',
            ),
            'where' => $where,
            'order' => 'article.listid asc',
            'offset' => 0,
            'limit' => $limit,
        );
        $result = $this->m_public->get_list($data, $page, $limit);
        return $result;
    }

    /**
     * 文章图片列表分页
     * @param int $articleid
     * @param int $page
     * @param int $limit
     * @return array
     */
    function get_articlepic_list_page($articleid, $page = 1, $limit = 10) {
        $where = array(
            'articlepic.isclose' => 0,
            'articlepic.articleid' => $articleid,
        );
        $data = array(
            'from' => 'articlepic',
            'where' => $where,
            'order' => 'articlepic.listid desc',
        );
        return $this->m_public->get_list_page($data, $page, $limit);
    }

    /**
     * 文章图片列表
     * @param int $articleid
     * @param int $limit 显示数量
     * @return array
     */
    function get_articlepic_list($articleid, $limit = 10) {
        $where = array(
            'articlepic.isclose' => 0,
            'articlepic.articleid' => $articleid,
        );
        $data = array(
            'from' => 'articlepic',
            'where' => $where,
            'order' => 'articlepic.listid desc',
            'offset' => 0,
            'limit' => $limit,
        );
        return $this->m_public->get_list($data);
    }

    /**
     * 获取文章
     * @param int $articleid
     * @return array
     */
    function get_article($articleid) {
        $data = array(
            'articleid' => $articleid,
        );
        return $this->m_public->getRowInfo('article', $data);
    }

    /**
     * 获取文章图片
     * @param int $articlepicid
     * @return array
     */
    function get_articlepic($articlepicid) {
        $data = array(
            'articlepicid' => $articlepicid,
        );
        return $this->m_public->getRowInfo('articlepic', $data);
    }

    /**
     * 获取片段内容
     * @param int $articlepicid
     * @return array
     */
    function get_block_list_page($page = 1, $limit = 10) {
        $where = array(
            'isclose' => 0
        );

        $data = array(
            'from' => 'block',
            'column' => 'blockid,blockname,blockcontent,addtime,',
            'where' => $where,
            'order' => 'listid desc',
            'offset' => 0,
            'limit' => $limit,
        );
        $result = $this->m_public->get_list_page($data, $page, $limit);
        return $result;
    }

    /**
     * 搜索文章列表
     * @param int $configid 网站id
     * @param int $isrecommend 是否推荐 1 推荐 ，2未推荐 0 所有
     * @param int $limit 显示数量
     * @return array 
     */
    function get_search_article_list($configid = 0, $isrecommend = 0, $page=1 , $limit = 10 , $expand=array()) {
        if(!empty($expand)){
            $where = $expand;
        }
        $where['isclose'] = 0;
        $where['isclose'] = $isrecommend;

        $data = array(
            'from' => 'article',
            'column' => 'articleid,classid,title,smallpic,bigpic,description,addtime',
            'where' => $where,
            'order' => 'article.listid asc',
            'offset' => 0,
            'limit' => $limit,
        );
        $result = $this->m_public->get_list_page($data, $page , $limit);
        return $result;
    }

}
